package com.glorysoft.sunnypda.ui.reportfault

import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import com.glorysoft.sunnypda.BR
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseActivity
import com.glorysoft.sunnypda.base.BaseViewModel
import com.glorysoft.sunnypda.base.Constant
import com.glorysoft.sunnypda.databinding.ActivityReportFaultBinding
import com.glorysoft.sunnypda.entity.EqpInfoEntity
import com.glorysoft.sunnypda.entity.ui.ToastMessageBean
import com.glorysoft.sunnypda.utils.PopWindowsUtils
import kotlinx.android.synthetic.main.activity_report_fault.*
import kotlinx.android.synthetic.main.activity_search_eqp_info.*

/**
 * FileName: ReportFaultActivity
 * Author: XJH
 * Date: 2022/1/7 15:50
 * Description:
 * History:
 */
class ReportFaultActivity : BaseActivity<ReportFaultViewModel,ActivityReportFaultBinding>(),
    View.OnClickListener {

    private var priorityPop: PopWindowsUtils.GeneralPop? = null
    private var typePop: PopWindowsUtils.GeneralPop? = null

    override fun layoutId(): Int {
        return R.layout.activity_report_fault
    }

    override fun initVariableId(): Int {
        return BR.reportFaultViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.fault_repair))
        tvUser.text = Constant.CURRENT_USER
        viewModel.mRepairFaultEntity.RFR_OP = Constant.CURRENT_USER
        initEdit(etEqpWork,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.initRepairForEqp(s)
            }

        })

        setRightListener(getString(R.string.confirm)){
            if (etEqpWork.text.toString().isEmpty()){
                viewModel.defUI.toastEvent.value = ToastMessageBean(getString(R.string.eqp_hint),false)
                return@setRightListener
            }
            if(tvType.text.toString() == "") {
                viewModel.mRepairFaultEntity.RFR_TYPE = ""
            }
            viewModel.reportFault(etEqpWork.text.toString())
        }

//        rbNormal.setOnClickListener(this)
//        rbEqp.setOnClickListener(this)
//        rbProgram.setOnClickListener(this)

        flTypeList.setOnClickListener(this)
        flPriorityList.setOnClickListener(this)
    }

    override fun initData() {
        viewModel.defUI.callObserve.observe(this){
            val eqpInfo = it.item as EqpInfoEntity
            viewModel.mRepairFaultEntity.EQP_TYPE = eqpInfo.EQPTYPE!!
            viewModel.mRepairFaultEntity.RPR_INPUT = eqpInfo.RPR_INPUT!!
            mBinding.setVariable(BR.reportFaultViewModel,viewModel)
        }
//        rbNormal.isChecked = true
//        viewModel.mRepairFaultEntity.RFR_TYPE = "0"
        viewModel.defUI.startActivity.observe(this){
            viewModel.defUI.toastEvent.value = ToastMessageBean("操作成功",true)
            setResult(RESULT_OK)
            finish()
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
//            R.id.rbNormal -> viewModel.mRepairFaultEntity.RFR_TYPE = "0"
//            R.id.rbEqp -> viewModel.mRepairFaultEntity.RFR_TYPE = "1"
//            R.id.rbProgram ->{
//            viewModel.mRepairFaultEntity.RFR_TYPE = "2"
//            }
            R.id.flTypeList -> {
                if (typePop == null) {
                    typePop = PopWindowsUtils.getOneChoosePop(
                        this,
                        flTypeList.width,
                        viewModel.typeList,
                        ivTypeArrow,
                        object :
                            PopWindowsUtils.PopTextAndPositionCallback {
                            override fun onSuccess(codeS: String, potion: Int) {
                                tvType.text = codeS
                                viewModel.mRepairFaultEntity.RFR_TYPE = codeS
                            }
                        })

                }

                if (typePop!!.isShowing) {
                    typePop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivTypeArrow)
                    typePop?.showAsDropDown(flTypeList, 0, 0)
                }
            }
            R.id.flPriorityList ->{
            if (priorityPop == null) {
                priorityPop = PopWindowsUtils.getOneChoosePop(
                    this,
                    flPriorityList.width,
                    viewModel.priorityList,
                    ivPriorityArrow,
                    object :
                        PopWindowsUtils.PopTextAndPositionCallback {
                        override fun onSuccess(codeS: String, potion: Int) {
                            tvPriority.text = codeS
                            viewModel.mRepairFaultEntity.PRIORITY = codeS
                        }

                    })

            }

            if (priorityPop!!.isShowing) {
                priorityPop?.dismiss()
            } else {
                PopWindowsUtils.setArrowShow(ivPriorityArrow)
                priorityPop?.showAsDropDown(flPriorityList, 0, 0)
            }
            }
        }
    }
}